package com.shanzmoo.platform.tomember.api.manager;


import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.db.module.product.domain.query.ScoreQueryVO;
import com.shanzmoo.db.module.product.domain.vo.ProductScoreVO;
import com.shanzmoo.db.module.product.domain.vo.ScoreDetailVO;
import com.shanzmoo.db.module.product.entity.ProductScoreEntity;
import com.shanzmoo.db.module.product.service.IProductScoreService;
import com.shanzmoo.db.module.product.service.IProductSkuService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ProductCenterManager {

    @Autowired
    private IProductScoreService productScoreService;

    @Autowired
    private IProductSkuService productSkuService;


    public SHttpResponse<List<ProductScoreVO>> getScoreSkuList(ScoreQueryVO scoreQueryVO) {
        List<ProductScoreVO> list = productScoreService.getScoreSkuPlatList(scoreQueryVO);
        return SHttpResponse.ok(list);
    }

    public SHttpResponse<ScoreDetailVO> getScoreInfo(Integer id) {
        ProductScoreEntity entity = productScoreService.getById(id);
        ScoreDetailVO scoreDetailVO = productScoreService.getScoreDetailBySku(entity.getSku());
//        JSONArray json = JSONArray.fromObject(scoreDetailVO.getCheckAttr());
//        String checkStr = handlerData(json);
//        scoreDetailVO.setCheckAttr(checkStr);
        return SHttpResponse.ok(scoreDetailVO);
    }

    private String handlerData(JSONArray json) {
        StringBuilder strB = new StringBuilder();
        for(int i=0;i<json.size();i++){
            JSONObject job = json.getJSONObject(i);
            String attrName = String.valueOf(job.get("attrName"));
            JSONArray checks = job.getJSONArray("checkAttributeValues");
            String attrValue = String.valueOf(checks.get(0));
            strB.append(attrName);
            strB.append(":");
            strB.append(attrValue);
            strB.append(";");
        }
        return strB.toString();
    }
}
